/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;

import nu.zoom.swing.desktop.plugin.portunis.KeyStoreListener;
import nu.zoom.swing.desktop.plugin.portunis.ZoomKeyStore;

/**
 * @see nu.zoom.swing.desktop.plugin.portunis.ZoomKeyStore
 * @version $Revision: 1.2 $
 * @author $Author: johan $
 */
public class ZoomKeyStoreImpl implements ZoomKeyStore
{

	private ArrayList<KeyStoreListener> listeners = new ArrayList<KeyStoreListener>();
	private KeyStore store;

	ZoomKeyStoreImpl(KeyStore store) {
		this.store = store;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#aliases()
	 */
	public Enumeration aliases() throws KeyStoreException
	{
		return store.aliases();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#containsAlias(java.lang.String)
	 */
	public boolean containsAlias(String alias) throws KeyStoreException
	{
		return store.containsAlias(alias);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#deleteEntry(java.lang.String)
	 */
	public void deleteEntry(String alias) throws KeyStoreException
	{
		store.deleteEntry(alias);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#getCertificate(java.lang.String)
	 */
	public Certificate getCertificate(String alias) throws KeyStoreException
	{
		return store.getCertificate(alias);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#getCertificateAlias(java.security.cert.Certificate)
	 */
	public String getCertificateAlias(Certificate cert)
			throws KeyStoreException
	{
		return store.getCertificateAlias(cert);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#getCertificateChain(java.lang.String)
	 */
	public Certificate[] getCertificateChain(String alias)
			throws KeyStoreException
	{
		return store.getCertificateChain(alias);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#getCreationDate(java.lang.String)
	 */
	public Date getCreationDate(String alias) throws KeyStoreException
	{
		return store.getCreationDate(alias);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#getKey(java.lang.String,
	 *      char[])
	 */
	public Key getKey(String alias, char[] password) throws KeyStoreException,
			NoSuchAlgorithmException, UnrecoverableKeyException
	{
		return store.getKey(alias, password);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#getProvider()
	 */
	public Provider getProvider()
	{
		return store.getProvider();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#getType()
	 */
	public String getType()
	{
		return store.getType();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#isCertificateEntry(java.lang.String)
	 */
	public boolean isCertificateEntry(String alias) throws KeyStoreException
	{
		return store.isCertificateEntry(alias);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#isKeyEntry(java.lang.String)
	 */
	public boolean isKeyEntry(String alias) throws KeyStoreException
	{
		return store.isKeyEntry(alias);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#load(java.io.InputStream,
	 *      char[])
	 */
	public void load(InputStream stream, char[] password)
			throws NoSuchAlgorithmException, CertificateException, IOException
	{
		store.load(stream, password);
		fireKeyStoreChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#setCertificateEntry(java.lang.String,
	 *      java.security.cert.Certificate)
	 */
	public void setCertificateEntry(String alias, Certificate cert)
			throws KeyStoreException
	{
		store.setCertificateEntry(alias, cert);
		fireKeyStoreChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#setKeyEntry(java.lang.String,
	 *      byte[], java.security.cert.Certificate[])
	 */
	public void setKeyEntry(String alias, byte[] key, Certificate[] chain)
			throws KeyStoreException
	{
		store.setKeyEntry(alias, key, chain);
		fireKeyStoreChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#setKeyEntry(java.lang.String,
	 *      java.security.Key, char[], java.security.cert.Certificate[])
	 */
	public void setKeyEntry(String alias, Key key, char[] password,
			Certificate[] chain) throws KeyStoreException
	{
		store.setKeyEntry(alias, key, password, chain);
		fireKeyStoreChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#size()
	 */
	public int size() throws KeyStoreException
	{
		return store.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#store(java.io.OutputStream,
	 *      char[])
	 */
	public void store(OutputStream stream, char[] password)
			throws KeyStoreException, NoSuchAlgorithmException,
			CertificateException, IOException
	{
		store.store(stream, password);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#addKeyStoreListener(nu.zoom.swing.desktop.plugin.portunis.crypto.KeyStoreListener)
	 */
	public synchronized void addKeyStoreListener(KeyStoreListener listener)
	{
		if (listener != null) {
			listeners.add(listener);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.plugin.portunis.crypto.ZoomKeyStore#removeKeyStoreListener(nu.zoom.swing.desktop.plugin.portunis.crypto.KeyStoreListener)
	 */
	public synchronized void removeKeyStoreListener(KeyStoreListener listener)
	{
		if (listener != null) {
			listeners.remove(listener);
		}
	}

	private synchronized void fireKeyStoreChanged()
	{
		for (int i = 0; i < listeners.size(); i++) {
			listeners.get(i).keyStoreChanged(this);
		}
	}
}
